package com.huobao.yunying.activitys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Temai_Detail;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.FristPageList;
import com.huobao.yunying.entity.TemaiDetail;
import com.huobao.yunying.entity.TemaiDetail_goods_list;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

/**
 * 首页特卖专场详情页
 * 
 * @author Administrator
 *
 */
public class Act_Temai_Detail extends HuobaoBaseActivity implements OnClickListener, OnScrollListener {

	@ViewInject(R.id.listview)
	private ListView listview;
	@ViewInject(R.id.btn_totop)
	private ImageView btn_toTop;
	/** 传过来得数据 */
	private FristPageList m_IntentData;

	private Http m_Http;
	private Adapter_Temai_Detail adapter;
	@ViewInject(R.id.rtly)
	private RelativeLayout layout;
	private ImageView m_HeadView;
	/**头部轮播*/
	private Sbanber sbanber;

	@Override
	protected int getContentView() {
		return R.layout.layout_listview;
	}

	@Override
	protected String getTitleText() {
		m_IntentData = (FristPageList) getIntent().getSerializableExtra("data");
		return m_IntentData.getAd_name();
	}

	@Override
	protected void init() {
		m_Http = Http.getInstant();
		adapter = new Adapter_Temai_Detail(context);
		btn_toTop.setOnClickListener(this);
		m_HeadView = new ImageView(context);
		listview.setOnScrollListener(this);
		listview.addHeaderView(m_HeadView);
		listview.setDividerHeight(8);
		listview.setAdapter(adapter);
		getData(true);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		btn_toTop.setVisibility(firstVisibleItem >= 1 ? View.VISIBLE : View.GONE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_totop:
			listview.smoothScrollToPosition(0);
			break;
		}
	}

	private void getData(boolean showDilaig) {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "special", "spec_id",
				m_IntentData.getAd_recomm(), "id", m_IntentData.getAd_id());
		m_Http.sendRequest(context, showDilaig,map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				// 背景色
				String  backcolor = jsonObject.getString("sbackcolor");
				layout.setBackgroundColor(Color.parseColor(backcolor));
				listview.setBackgroundColor(Color.parseColor(backcolor));
				// 头部图片
				JSONArray jsonArray = jsonObject.getJSONArray("sbanber");
				sbanber = new Sbanber();
				sbanber.ad_banber = ((JSONObject) jsonArray.get(0)).getString("ad_banber");
				sbanber.ad_id = ((JSONObject) jsonArray.get(0)).getString("ad_id");
				ImageUtils.loadimg(m_HeadView, sbanber.ad_banber, new OnImageLoadListener() {
					
					@Override
					public void onLoadSuccess(Drawable drawable) {
						int newWidth = ScreenUtil.getInstant().getDisWidth(context);
						int newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
						LayoutParams layoutParams = new LayoutParams(newWidth, newHight);
						m_HeadView.setLayoutParams(layoutParams);
					}
				});
				//grid
				jsonArray = jsonObject.getJSONArray("sgoodlist");
				List<TemaiDetail> list = new ArrayList<TemaiDetail>();
				for(int i=0;i<jsonArray.size();i++){
					JSONObject object = jsonArray.getJSONObject(i);
					TemaiDetail detail = new TemaiDetail();
					detail.setRecom_id(object.getString("recom_id"));
					detail.setRecom_logo(object.getString("recom_logo"));
					detail.setRecom_name(object.getString("recom_name"));
					detail.setKeywords(object.getString("keywords"));
					detail.setBrand(object.getString("brand"));
					detail.setRecomm_link(object.getString("recomm_link"));
					detail.setGoods_list(JSON.parseArray(object.getString("goods_list"), TemaiDetail_goods_list.class));
					list.add(detail);
				}
				adapter.clear();
				adapter.addAll(list);
				adapter.notifyDataSetChanged();
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}
	
//	@Override
//	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
//		super.onActivityResult(arg0, arg1, arg2);
//		LogUtil.e("arg0="+arg0);
//		if(arg0 == 1){
//			getData(false);
//		}
//	}

	private class Sbanber {
		public String ad_banber;
		public String ad_id;
	}
}
